package com.base.controller.czb.czbadmin;

import com.base.po.czb.GasOrderH5read;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.CZB.GasOrderH5readService;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import com.base.utils.Page;
import com.base.utils.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/gas")
public class H5GasOrderControllerAPI {
    @Autowired
    SubscribersService subscribersService;
    @Autowired
    UsersService usersService;
    @Autowired
    GasOrderH5readService gasOrderH5readService;

    @RequestMapping("/content_gasorderH5")
    public @ResponseBody
    Map<String, Object>
    index(HttpServletRequest request, Integer currentPageNo,
          String mobile, String czborderid, String adminUserId, String parentid, String gasid, String paystatus, String createTime1, String createTime2) throws Exception {
        Map<String, Object> map = new HashMap<>(); // 返回数据对象
        if (adminUserId == null) {
            return map;
        }

        if (currentPageNo == null) {
            currentPageNo = 1;
        }
        if (paystatus == null || paystatus.equals("") || paystatus.equals("0")) {
            paystatus = null;
        }

        if (parentid == null || parentid.equals("")) {
            parentid = null;
        }else{
            try{
                Users agent=usersService.selectByUsername(parentid);
                if (agent==null){
                    map.put("msg", "商户不存在");
                    return map;
                }
                parentid=agent.getId();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        Subscribers subscribers;
        String userId = null;

        if (mobile == null || mobile.equals("")) {
            mobile = null;
        } else {
            if (!Produce.isMobile(mobile)) {
                map.put("msg", "请输入正确的手机号");
                return map;
            }
            subscribers = subscribersService.selectBySubscribers(mobile, adminUserId);
            if (subscribers == null) {
                userId = null;
            } else {
                userId = subscribers.getId();
            }
        }

        if (czborderid == null || czborderid.equals("")) {
            czborderid = null;
        }

        if (gasid == null || gasid.equals("")) {
            gasid = null;
        }

        if (paystatus == null || paystatus.equals("")) {
            paystatus = null;
        }

        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        if (currentPageNo != null) page.setCurrentPageNo(currentPageNo); // 设置当前页

        page.setCurrentPage((currentPageNo - 1) * page.getPageSize());

        Integer gasOrderCount = gasOrderH5readService.countAllGasOrder(parentid, czborderid, mobile, gasid, paystatus, createTime1, createTime2);

        List<GasOrderH5read> gasOrders = gasOrderH5readService.selectAllGasOrder(parentid, czborderid,mobile, gasid, paystatus, createTime1, createTime2, page);

        gasOrders.forEach(item->{
            if (item.getParentid()!=null && !item.getParentid().equals("")){
                String parentid1=item.getParentid();
                Users users=usersService.selectByPrimaryKey(parentid1);
                item.setParentName(users.getUsername());
            }
        });
        page.setRecordTotal(gasOrderCount); // 设置总条数
        map.put("msg", "查询成功");
        map.put("mobile", mobile);
        map.put("czborderid", czborderid);
        map.put("payParentId", parentid);
        map.put("paystatus", paystatus);
        map.put("gasOrderList", gasOrders); // 提现集合
        map.put("pages", page); // 分页
        return map;
    }
}
